package cn.two.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.two.dao.OaBuyDao;
import cn.two.model.OaBuy;
import cn.two.service.OaBuyService;

@Service("oaBuyService")
public class OaBuyServiceImpl implements OaBuyService {

	@Resource
	private OaBuyDao oaBuyDao;

	/**
	 * 查询所有
	 */
	public List<OaBuy> selectAllBuy() {

		return oaBuyDao.selectAllBuy();
	}

	/**
	 * 添加
	 * 
	 */
	public void save(OaBuy oaBuy) {
		oaBuyDao.save(oaBuy);

	}

	/**
	 * 根据id删除
	 */

	public void delete(OaBuy oaBuy) {
		oaBuyDao.delete(oaBuy);

	}

	/**
	 * 多条件查询
	 */
	public List<OaBuy> search(OaBuy oaBuy) {

		return oaBuyDao.search(oaBuy);

	}

	/**
	 * 查询申请
	 */
	public OaBuy findOaBuy(OaBuy oaBuy) {
		
		return oaBuyDao.findOaBuy(oaBuy);
	}

	/**
	 * 修改
	 */
	public void update(OaBuy oaBuy) {
		oaBuyDao.update(oaBuy);
	}

	public Page queryLikeOaBuys(Map<String, Object> cond) {
		Page page = new Page();
		// 根据条件查询符合的用户列表记录总数，赋值给page的totalNum变量
		page.setTotal(oaBuyDao.getLikeOaBuysCount(cond));
		// 从请求参数中获取每页大小
		int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize")));
		page.setPageSize(pageSize);
		// 从请求参数中获取当前页码
		int curPageNum = Integer.parseInt(String.valueOf(cond.get("pageNum")));
		page.setPageNum(curPageNum);
		// 动态计算总页数(总记录数 除以 每页大小，加上 总页数 求余 每页大小，如果余数不为0，则 加 1，否则 加 0 )
		page.setPageNum((int) (page.getTotal() / pageSize + (page.getTotal()
				% pageSize == 0 ? 0 : 1)));
		// 根据条件查询符合的用户列表记录，赋值给page的result变量
		page = PageHelper.startPage(curPageNum, pageSize);
		List list = oaBuyDao.getLikeOaBuys(cond);

		return page;
	}

	public OaBuyDao getOaBuyDao() {
		return oaBuyDao;
	}

	public void setOaBuyDao(OaBuyDao oaBuyDao) {
		this.oaBuyDao = oaBuyDao;
	}

}
